package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.client.StationClient;
import com.woniu.client.dto.StationDto;
import com.woniu.comment.util.StateData;
import com.woniu.exception.RebornException;
import com.woniu.mapper.DepotDetialMapper;
import com.woniu.mapper.DepotMapper;
import com.woniu.mapper.JunkTypeMapper;
import com.woniu.model.Depot;
import com.woniu.model.DepotDetial;
import com.woniu.model.Result;
import com.woniu.model.Review;
import com.woniu.service.DepotService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author eason
 * @since 2024年05月23日
 */
@Service
public class DepotServiceImpl extends ServiceImpl<DepotMapper, Depot> implements DepotService {

    @Resource
    private DepotDetialMapper depotDetialMapper;
    @Resource
    private DepotMapper depotMapper;
    @Resource
    private JunkTypeMapper junkTypeMapper;
    @Resource
    private StationClient stationClient;

    /**
     * 给某个站点新增仓库
     * @param id 站点id
     */
    @Override
    public void add(Integer id) {
        Result<StationDto> byStationId = stationClient.findByStationId(id);
        if (byStationId.getCode()!=200){
            throw new RebornException(byStationId.getMessage(),byStationId.getCode());
        }
        StationDto station = byStationId.getData();
        if (station==null){
            throw new RebornException("不存在的站点",60001);
        }
        Depot depot = depotMapper.selectOne(new QueryWrapper<Depot>().eq("station_id", id));
        if (depot!=null){
            throw new RebornException("该站点已存在仓库,无法再次添加",50001);
        }
        Depot depot1 = new Depot();
        depot1.setStationId(id);
        depotMapper.insert(depot1);
    }

    /**
     * 根据站点ID查找相关仓库详细信息
     *
     * @param id 站点ID
     * @return 与该站点相关的仓库详细信息列表
     * @throws RuntimeException 如果站点ID错误或该站点无仓库
     */
    @Override
    public List<DepotDetial> findByStationId(Integer id) {
        List<Depot> depots = depotMapper.selectList(new QueryWrapper<Depot>().eq("station_id", id));
//        if (depots.isEmpty()){
//            throw new RebornException("站点id错误或该站点无仓库",60000);
//        }
        List<DepotDetial> list =new ArrayList<>();
        if (depots!=null&&!depots.isEmpty()){
            list = depotDetialMapper.selectList(new QueryWrapper<DepotDetial>().eq("depot_id", depots.get(0).getId()));
        }
        return list;
    }

    /**
     * 根据stationId查找page
     * id:站点id
     */
    @Override
    public Page<DepotDetial> findByStationIdPage(Integer id, Integer pageNum, Integer pageSize) {
        // 默认分页参数处理
        pageNum = (pageNum == null) ? 1 : pageNum;
        pageSize = (pageSize == null) ? 3 : pageSize;

        Page<DepotDetial> page = new Page<>(pageNum, pageSize);
        // 创建查询条件
        QueryWrapper<DepotDetial> wrapper = new QueryWrapper<>();
        wrapper.eq("station_id", id);

        // 执行分页查询
        Page<DepotDetial> detialPage = depotDetialMapper.selectPage(page, wrapper);

        // 检查结果是否为空
        if (detialPage == null || page.getRecords().isEmpty()) {
            throw new RebornException("该站点没有任何库存详情", 60001);
        }
        // 返回分页结果
        return detialPage;
    }
}
